<template>
  <div class="app-container">
    <!-- 查询行 -->
    <el-row :gutter="10" class="mb8 query-row">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="日志类型" prop="logType">
          <el-select v-model="queryParams.logType" placeholder="请选择日志类型" clearable size="mini"
            @keyup.enter.native="handleQuery">
            <el-option v-for="option in logTypeOptions" :key="option.value" :label="option.label" :value="option.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程编码" prop="flowCode">
          <el-input v-model="queryParams.flowCode" placeholder="请输入流程编码" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="流程ID" prop="flowId">
          <el-input v-model="queryParams.flowId" placeholder="请输入流程ID" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="接口名称" prop="apiName" v-show="showSearch">
          <el-input v-model="queryParams.apiName" placeholder="请输入接口名称" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="返回消息" prop="retmsg" v-show="showSearch">
          <el-input v-model="queryParams.retmsg" placeholder="请输入返回消息" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="创建时间" v-show="showSearch">
          <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 按钮行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['aitcommon:aitlog:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain size="mini" :loading="exportLoading" @click="handleFlowInfo">
          <svg-icon icon-class="documentation" /> 查看流程配置({{ currentRow && currentRow.id }})
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain size="mini" :loading="exportLoading" @click="handleRestInfo">
          <svg-icon icon-class="documentation" /> 查看REST调用({{ currentRow && currentRow.id }})
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aitlogList" @selection-change="handleSelectionChange"
      @row-dblclick="handleDetail" :row-style="{ height: '32px' }" :cell-style="{ padding: '0px' }" border
      :height="tableHeight" :row-class-name="tableRowClassName" highlight-current-row
      @current-change="handleCurrentChange">
      <el-table-column label="ID" width="120" align="center" prop="id" fixed />
      <el-table-column label="日志类型" width="120" align="center" prop="logType" show-overflow-tooltip fixed />
      <el-table-column label="接口名称" width="120" align="center" prop="apiName" show-overflow-tooltip fixed />
      <el-table-column label="耗时(毫秒)" width="120" align="center" prop="runTime" show-overflow-tooltip />
      <el-table-column label="返回码" width="120" align="center" prop="retcode" show-overflow-tooltip />
      <el-table-column label="返回消息" width="300" align="left" prop="retmsg" show-overflow-tooltip />
      <el-table-column label="流程ID" width="180" align="center" prop="flowId" show-overflow-tooltip />
      <el-table-column label="流程编码" width="180" align="center" prop="flowCode" show-overflow-tooltip />
      <el-table-column label="流程名称" width="120" align="center" prop="flowName" show-overflow-tooltip />
      <el-table-column label="创建时间" width="180" align="center" prop="createTime" show-overflow-tooltip />
      <el-table-column label="传入参数" width="300" align="left" prop="inParam" show-overflow-tooltip />
      <el-table-column label="返回参数" width="300" align="left" prop="retParam" show-overflow-tooltip />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 接口日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本" name="base">
            <el-col :span="24">
              <el-form-item label="接口名称" prop="apiName"> {{ form.apiName }} </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="接口方法" prop="apiMethod"> {{ form.apiMethod }} </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返回码" prop="retcode"> {{ form.retcode }} </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返回消息" prop="retmsg">
                <el-input v-model="form.retmsg" type="textarea" placeholder="请输入内容" :rows="6" readonly />
              </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="流程信息" name="flow">
            <el-col :span="12">
              <el-form-item label="流程id" prop="flowId"> {{ form.flowId }} </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流程编码" prop="flowCode"> {{ form.flowCode }} </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流程名称" prop="flowName"> {{ form.flowName }} </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="服务信息" name="controller">
            <el-col :span="6">
              <el-form-item label="请求IP" prop="operIp"> {{ form.operIp }} </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="请求URL" prop="operUrl" class="single-content"> {{ form.operUrl }} </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="请求方法" prop="reqMethod"> {{ form.reqMethod }} </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="头信息" prop="reqHead" class="single-content"> {{ form.parField0 }} </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="参数" name="param">
            <el-col :span="24">
              <el-form-item label="传入参数" prop="inParam">
                <el-input v-model="form.inParam" type="textarea" placeholder="请输入内容" :rows="6" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返回参数" prop="retParam">
                <el-input v-model="form.retParam" type="textarea" placeholder="请输入内容" :rows="6" readonly />
              </el-form-item>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="其他" name="other">
            <el-col :span="12">
              <el-form-item label="参数字段0" prop="parField0" v-show="showDetail">
                <el-input v-model="form.parField0" placeholder="请输入参数字段0" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段1" prop="parField1" v-show="showDetail">
                <el-input v-model="form.parField1" placeholder="请输入参数字段1" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段2" prop="parField2" v-show="showDetail">
                <el-input v-model="form.parField2" placeholder="请输入参数字段2" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段3" prop="parField3" v-show="showDetail">
                <el-input v-model="form.parField3" placeholder="请输入参数字段3" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段4" prop="parField4" v-show="showDetail">
                <el-input v-model="form.parField4" placeholder="请输入参数字段4" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段5" prop="parField5" v-show="showDetail">
                <el-input v-model="form.parField5" placeholder="请输入参数字段5" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段6" prop="parField6" v-show="showDetail">
                <el-input v-model="form.parField6" placeholder="请输入参数字段6" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段7" prop="parField7" v-show="showDetail">
                <el-input v-model="form.parField7" placeholder="请输入参数字段7" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段8" prop="parField8" v-show="showDetail">
                <el-input v-model="form.parField8" placeholder="请输入参数字段8" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="参数字段9" prop="parField9" v-show="showDetail">
                <el-input v-model="form.parField9" placeholder="请输入参数字段9" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段0" prop="retField0" v-show="showDetail">
                <el-input v-model="form.retField0" placeholder="请输入返回字段0" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段1" prop="retField1" v-show="showDetail">
                <el-input v-model="form.retField1" placeholder="请输入返回字段1" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段2" prop="retField2" v-show="showDetail">
                <el-input v-model="form.retField2" placeholder="请输入返回字段2" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段3" prop="retField3" v-show="showDetail">
                <el-input v-model="form.retField3" placeholder="请输入返回字段3" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段4" prop="retField4" v-show="showDetail">
                <el-input v-model="form.retField4" placeholder="请输入返回字段4" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段5" prop="retField5" v-show="showDetail">
                <el-input v-model="form.retField5" placeholder="请输入返回字段5" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段6" prop="retField6" v-show="showDetail">
                <el-input v-model="form.retField6" placeholder="请输入返回字段6" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段7" prop="retField7" v-show="showDetail">
                <el-input v-model="form.retField7" placeholder="请输入返回字段7" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段8" prop="retField8" v-show="showDetail">
                <el-input v-model="form.retField8" placeholder="请输入返回字段8" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="返回字段9" prop="retField9" v-show="showDetail">
                <el-input v-model="form.retField9" placeholder="请输入返回字段9" />
              </el-form-item>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
        <!--
        <el-button @click="showDetailClick" v-show="!showDetail"
          >详 细</el-button
        >
        <el-button @click="showDetailClick" v-show="showDetail"
          >主 要</el-button
        >
        -->
      </div>
    </el-dialog>

    <flow-info-dlg :visible.sync="flowOpen" :code="currentRow && currentRow.flowCode"></flow-info-dlg>
    <!-- 查看RestCall信息 -->
    <RestInfoDLG ref="RestInfoDLG"></RestInfoDLG>
  </div>
</template>

<script>
import {
  listAitlog,
  getAitlog,
  delAitlog,
  addAitlog,
  updateAitlog,
  exportAitlog,
} from "@/api/aitcommon/aitlog";
import FlowInfoDlg from '@/components/AIT/FlowInfo';
import { showSettings } from "../../../settings";
import RestInfoDLG from '@/components/AIT/RunTimeDlg/RestInfo';

export default {
  name: "aitlog",
  components: { FlowInfoDlg,RestInfoDLG },
  data() {
    return {
      // 日期范围
      dateRange: [],

      logTypeOptions: [{ value: "服务日志", label: '服务日志' }, 
        { value: "流程日志", label: '流程日志' },
        { value: "HTTP日志", label: 'HTTP日志' }],
      // 当前行
      currentRow: null,
      // 流程详情
      flowOpen: false,
      //表格高度
      tableHeight: 500,
      // 默认页签
      activeName: 'base',
      // 表单详细
      showDetail: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 接口日志表格数据
      aitlogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    // console.info('created:',this.queryParams)
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      // window.innerHeight 浏览器窗口的可见高度，下面的 220 是除了table最大高度的剩余空间。
      let height = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 220;
      this.tableHeight = height;
    })
  },
  watch: {
    // 监控路由
    // 第一次页面创建进入created,不进入这里
    "$route"(to, from) {
      // console.info('watch:', this.queryParams)
      if (to.path === '/ait/aitlog') {
        this.getList();
      }
    }
  },
  methods: {
    handleRestInfo() {
      // 通过调用子控件方法,弹出窗口,并传递参数
      this.$refs.RestInfoDLG.openLogDlg(this.currentRow && this.currentRow.id);
    },
    handleCurrentChange(val) {
      // console.info('handleCurrentChange',val)
      this.currentRow = val;
      // val && this.$refs.mainTable.toggleRowSelection(val);
    },
    handleFlowInfo() {
      if (!this.currentRow) {
        this.$message({
          message: '请先选择一条记录!',
          type: 'warning'
        });
        return;
      }
      // console.info('handleFlowInfo',this.currentRow)
      this.flowOpen = true;
    },
    showDetailClick() {
      this.showDetail = !this.showDetail;
      //console.info('this.showDetail:',this.showDetail);
    },
    /** 查询接口日志列表 */
    getList() {
      this.loading = true;
      // 通过路由构造查询
      if (!this.queryParams.flowId && this.$route.query.flowId)
        this.queryParams.flowId = this.$route.query.flowId;
      if (!this.queryParams.retcode && this.$route.query.retcode)
        this.queryParams.retcode = this.$route.query.retcode;
      if ((this.dateRange==undefined||this.dateRange.length==0) && this.$route.query.beginTime)
        this.dateRange = [this.$route.query.beginTime, this.$route.query.endTime];
      
      // 调用查询服务
      let query = this.addDateRange(this.queryParams, this.dateRange)
      // console.info('this.dateRange',this.dateRange)
      listAitlog(query).then((response) => {
        this.aitlogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
      };
      this.dateRange = [];
      this.resetForm("queryForm");
      
      // 判断路由是否为空
      if (Object.keys(this.$route.query).length == 0)
        // 如果路由为空,直接查询
        this.handleQuery();
      else
        // 如果路由不空,清除路由
        this.$router.push({ query: {} })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      getAitlog(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "接口日志详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAitlog(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAitlog(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有接口日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportAitlog(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => { });
    },
    tableRowClassName({ row, rowIndex }) {
      // console.info('tableRowClassName',row)
      if (row.retcode === '200') {
        return 'normal-row';
      } else {
        return 'abnormal-row';
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.single-content {
  /* 文本不换行 */
  white-space: nowrap;
  /* 超出隐藏 */
  // overflow: hidden;
  overflow: auto;
  /* 超出的文字省略号表示 */
  text-overflow: ellipsis;
}
</style>
